package cn.xo68.democacheredis.service;

import cn.xo68.democacheredis.controller.IndexController;
import cn.xo68.democacheredis.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@CacheConfig(cacheNames = {"userCache"})
@Service
public class UserSevice {

    private static  final Logger logger= LoggerFactory.getLogger(UserSevice.class);

    @CachePut(key = "#user.userId")
    public User add(User user){
        logger.info("添加用户，userId:{},userName:{}",user.getUserId(),user.getUserName());
        return user;
    }
    @Cacheable(key = "#userId")
    public User get(String userId){
        User user=new User("0","defaultName");
        logger.info("读取用户，userId:{},userName:{}",user.getUserId(),user.getUserName());
        return user;
    }

    @CacheEvict(key = "#userId")
    public void delete(String userId){
        logger.info("删除用户，userId:{}",userId);
    }
}
